// PROGRAMA : SISPC001 OPCAO: 'COS'
// BIBLIOTECA : PRODETR
// SISTEMA : INFORMACOES DE SEGURANCA PUBLICA - SISP
// CLIENTE : SEJUSP - SEC. DA JUSTICA E SEGURANCA PUBLICA
// INSTITUTO DE IDENTIFICACAO
// PROGRAMADOR : PERILO SERGIO DATA: 12/12/1999
// OBJETIVO : EMISSAO/CONSULTA OPERACOES DO SISTEMA.
// ATUALIZACOES DATA
// PROGRAMA : SISPC001 26/08/1999
// BIBLIOTECA : SISP
// PROGRAMADOR :
// OBJETIVO : ATUALIZAR PROGRAMA, PARA SEPARACAO DOS MODULOS
// : ATUALIZAR DATAS (N6) PARA (N8) E TELAS 19/10/1999
package br.com.vti.sisp_sicrim.antigo.controller.programa;

import java.io.FileNotFoundException;
import java.text.ParseException;
import java.util.Arrays;

import net.sf.jasperreports.engine.JRException;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.vti.sisp_sicrim.antigo.controller.relatorios.Sistc101Report;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Sistc001;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Sistc001B;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Sistc001C;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Sistc002;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Sistc002B;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Sistc002C;
import br.com.vti.sisp_sicrim.antigo.model.SicOperacao;
import br.com.vti.sisp_sicrim.antigo.model.Sislac01;
import br.com.vti.sisp_sicrim.antigo.util.ExceptionProgram;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;
import br.com.vti.sisp_sicrim.antigo.util.Sisgsisp;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;

/**
 * Classe : Sispc001 OBJETIVO : EMISSAO/CONSULTA OPERACOES DO SISTEMA. *
 * 
 * @author Incode - Data: mai/2014
 * 
 */
@Resource
@Path("/antigo/sispc001")
public class Sispc001 {
	// Views utilizadas
	SicOperacao vwDveope = new SicOperacao();
	// Telas/relatorios utilizados
	Sistc001 sistc001;
	Sistc001B sistc001B;
	Sistc101Report report;
	// Sistc001C sistc001C;
	Sistc002 sistc002;
	Sistc002B sistc002B;
	// Sistc002C sistc002C;
	// Includes utilizados
	Sislac01 sislac01 = new Sislac01();
	// Areas globais / session
	Sisgsisp sisgsisp;
	// Funcoes/subrotinas
	FuncoesGerais funcoes = new FuncoesGerais();

	Result result;
	Sessao sessao;
	Boolean primeiraVez = false;
	Sispc001 sispc001Anterior;
	String faseproc = "0";

	private int modulo = 0;
	private int i = 0;
	private int j = 0;
	private int w = 0;
	private String[] tbo = { "", "VEI", "CON", "MUL", "ARR", "IPV", "ROU", "PMM", "VIS", "TAB", "SEN", "CRI", "IDC", "REN", "FUN", "BAT", "RCH", "RFR", "RPM", "SUP", "CCM", "PRF" };
	private String pesq = "";
	private String descel = "";
	int md = 0;

	private Sistc002 preMapas() throws ParseException {
		int i1, i2;
		sistc002 = new Sistc002(true, sessao);
		sistc001 = new Sistc001(true, sessao);
		sistc001B = new Sistc001B(true, sessao);
		// sistc001C = new Sistc001C(true, sessao);
		sistc002B = new Sistc002B(true, sessao);
		// sistc002C = new Sistc002C(true, sessao);
		for (i1 = 1; i1 <= 14; i1++) {
			sistc001.setOpe0(i1, "");
			sistc001.setMod0(i1, "");
			sistc001.setFin0(i1, "");
			sistc001.setPro0(i1, "");
			sistc001.setDes0(i1, "");
			sistc001.setAtu0(i1, 0);
			sistc001.setMat0(i1, 0);
			sistc001.setFun0(i1, "");
			report.setOpe9(i1, "");
			report.setMod9(i1, "");
			report.setFin9(i1, "");
			report.setPro9(i1, "");
			report.setDes9(i1, "");
		}
		Arrays.fill(tbo, "");
		sistc002.setDataCorrente(UtilBean.inverteDataCorrente());

		// Par�grafo: r0()
		return sistc002;
	}

	public Sispc001(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	@Post
	public void executar(Sistc001 sistc001, Sistc001B sistc001B, Sistc001C sistc001C, Sistc002 sistc002, Sistc002B sistc002B, Sistc002C sistc002C) {
		try {
			main(sistc001, sistc001B, sistc001C, sistc002, sistc002B, sistc002C);
		} catch (Exception e) {
			ExceptionProgram excp = new ExceptionProgram();
			excp.trataErroSiscerro(e, sessao, this.getClass().getSimpleName());
		}
	}

	private void main(Sistc001 psistc001, Sistc001B psistc001B, Sistc001C psistc001C, Sistc002 psistc002, Sistc002B psistc002B, Sistc002C psistc002C) throws Exception {
		int i1 = 0;
		if (psistc001 == null && psistc001B == null && psistc001C == null && psistc002 == null && psistc002B == null && psistc002C == null) {
			primeiraVez = true;
			// sistc002 = new Sistc002 (primeiraVez);
			sistc002 = preMapas();
			sistc002.setNavegar("../sispc001/executar");
			result.include("sistc002", sistc002);
			sessao.setAtributo("SISPC001", this);
			sistc002.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return;
		} else {
			restaurarEstados(psistc001, psistc001B, psistc001C, psistc002, psistc002B, psistc002C);
		}

		// tela = "";
		// DEFINE PRINTER(EFM = 1)
		for (i1 = 1; i1 <= 14; i1++) {
			report.setOpe9(i1, "");
			report.setMod9(i1, "");
			report.setFin9(i1, "");
			report.setPro9(i1, "");
			report.setDes9(i1, "");
		}

		sistc002.setMensagemSaida("SE MODULO NAO INFORMADO SERA EFETUADA PESQUISA SEQUENCIAL");

		sistc001.setConf("S");
		for (i1 = 1; i1 <= 2; i1++) {
			sistc002.setConimp(i1, "");
		}

		sistc002.receberTela(false, result);
		if (UtilBean.validaSelecao(sistc002.getConimp())) {
			sistc002.enviarMensagem(null, "INFORME UMA OPCAO");
			return;
		}
		if (sistc001B.getModulo() > 0) {
			sistc002.setMod1(tbo[sistc001B.getModulo()]);
			md = sistc001B.getModulo() + 1;
			// UtilBean.subStr(mdfuop,0, 2) = sistc001B.getModulo();
		}
		sistc001.setMensagem("");
		if (!sistc002.getConimp(1).trim().equals("")) {
			w = 14;
		} else {
			w = 46;
		}
		// while( vwDveope.leituraMdfuopMaiorig(pFuncao, pOperacao,
		// sistc001B.getModulo())
		while (vwDveope.leituraMdfuopMaiorig("", "", md))
		// while (vwDveope.leituraMdfuopMaiorig (mdfuop))
		{
			if (sistc001B.getModulo() > 0) {
				if (!(vwDveope.getModulo() == sistc001B.getModulo() + 1)) {
					continue;
				}
			}
			i += 1;
			if (w == 14) {
				sistc001.setOpe0(i, vwDveope.getOperacao());
				// DISPLAY DIAOPE-OPERAC
				sistc001.setMod0(i, tbo[vwDveope.getModulo() - 1]);
				// IF DINOPE-MODULO < 2
				sistc001.setFin0(i, vwDveope.getFuncao());
				// END-IF
				sistc001.setPro0(i, vwDveope.getPrograma());
				sistc001.setDes0(i, vwDveope.getDescricao());
				sistc001.setAtu0(i, vwDveope.getDataAtu());
				sistc001.setAtu0(i, (int) funcoes.sissd002(sistc001.getAtu0(i)));
				sistc001.setMat0(i, vwDveope.getMatriculaFunc());
				pesq = UtilBean.moveSubstring(pesq, 0, 3, "N", 18);
				pesq = UtilBean.moveSubstring(pesq, 3, 12, "N", vwDveope.getMatriculaFunc());
				descel = funcoes.sisst001(18, vwDveope.getMatriculaFunc(), descel);
				sistc001.setFun0(i, UtilBean.subStr(descel, 0, 30));
				for (j = 1; j <= 18; j++) {
					if (vwDveope.getModdis(j) > 0) {
						sistc001.setDis0(j, i, "S");
					} else {
						sistc001.setDis0(j, i, "N");
					}
				}
				if (i == 14) {
					tela(psistc001);
					if (sistc001.getConf().equals("N")) {
						sistc002.setMod1("");
						modulo = 0;
						break;
					}
				}
			} else {
				report.setOpe9(i, vwDveope.getOperacao());
				report.setMod9(i, tbo[vwDveope.getModulo() - 1]);
				report.setFin9(i, vwDveope.getFuncao());
				report.setPro9(i, vwDveope.getPrograma());
				report.setDes9(i, vwDveope.getDescricao());
				for (j = 1; j <= 18; j++) {
					if (vwDveope.getModdis(j) > 0) {
						report.setDis9(j, i, "S");
					} else {
						report.setDis9(j, i, "N");
					}
				}
				if (i == 46) {
					tela(psistc001);
				}
			}
		}

		if (i > 0) {
			sistc002.setMensagemSaida("FINAL DE ARQUIVO");
			tela(psistc001);
		}
		sessao.setAtributo("SISPC001", this);
		result.include("sistc002", sistc002);
		sistc002.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
	}

	private void tela(Sistc001 psistc001) throws ParseException, JRException, FileNotFoundException {
		if (w == 14) {
			if (!sistc001.getEnviada()) {
				sessao.setAtributo("SISPC001", this);
				result.include("sistc001", sistc001);
				sistc001.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (psistc001 != null) {
				sistc001.receberTela(false, result);
			}
			i = 0;
			// tela = "";
		} else {
			report.setUfloca(sessao.getSisgsisp().getUfloca());
			report.setHoraCorrente(sistc001.getHoraCorrente());
			report.gravaRelatorio(1);
			report.imprimir();
			// RELATORIO
			i = 0;
			// tela = "";
		}
	}

	private void restaurarEstados(Sistc001 psistc001, Sistc001B psistc001B, Sistc001C psistc001C, Sistc002 psistc002, Sistc002B psistc002B, Sistc002C psistc002C) {
		this.sispc001Anterior = (Sispc001) sessao.getAtributo("SISPC001");
		if (psistc001 != null) {
			sistc001 = psistc001;
		} else {
			sistc001 = this.sispc001Anterior.sistc001;
		}
		sistc001.setSessao(sessao);
		if (psistc001B != null) {
			sistc001B = psistc001B;
		} else {
			sistc001B = this.sispc001Anterior.sistc001B;
		}
		sistc001B.setSessao(sessao);
		if (psistc002 != null) {
			sistc002 = psistc002;
		} else {
			sistc002 = this.sispc001Anterior.sistc002;
		}
		sistc002.setSessao(sessao);
		if (psistc002B != null) {
			sistc002B = psistc002B;
		} else {
			sistc002B = this.sispc001Anterior.sistc002B;
		}
		sistc002B.setSessao(sessao);
		this.faseproc = this.sispc001Anterior.faseproc;
	}
}
